//
//  UTTranslatorViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/3.
//

import UIKit
import Toast_Swift

class UTTranslatorViewController: UTBaseViewController {
    
    @IBOutlet weak var navigationHeight: NSLayoutConstraint!
    @IBOutlet weak var bgView: UIImageView!
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var dayLabel: UILabel!
    @IBOutlet weak var monthYearLabel: UILabel!
    @IBOutlet weak var enLabel: UILabel!
    @IBOutlet weak var cnLabel: UILabel!
    @IBOutlet weak var translateButton: UIButton!
    
    lazy var resourceDatas: [[String: Any]] = {
        let list = [
            [
                "bg": "pic_mryj_bg_1",
                "icon": "pic_mryj_1",
            ],
            [
                "bg": "pic_mryj_bg_2",
                "icon": "pic_mryj_2",
            ],
            [
                "bg": "pic_mryj_bg_3",
                "icon": "pic_mryj_3",
            ],
            [
                "bg": "pic_mryj_bg_4",
                "icon": "pic_mryj_4",
            ],
            [
                "bg": "pic_mryj_bg_5",
                "icon": "pic_mryj_5",
            ],
        ]
        return list
    }()
    
    lazy var currentResource: [String: Any] = {
        let randomIndex = Int.random(in: 0..<resourceDatas.count)
        return resourceDatas[randomIndex]
    }()
    
    
    lazy var datas: [UTTranslatorModel] = {
        if let list = UTTranslatorModel.loadLocalJSON(filename: "proverbs") {
            return list
        }
        return Array<UTTranslatorModel>()
    }()
    
    lazy var currentData: UTTranslatorModel? = {
        guard !datas.isEmpty else { return nil }
        let randomIndex = Int.random(in: 0..<datas.count)
        return datas[randomIndex]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }
    
    fileprivate func setupViews() {
        navigationHeight.constant = UIApplication.shared.statusBarFrame.size.height + 44
        bgView.image = UIImage(named: currentResource["bg"] as! String)
        iconView.image = UIImage(named: currentResource["icon"] as! String)
        let currentDate = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day, .month, .year], from: currentDate)
        if let day = components.day {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM"
            let monthAbbreviation = dateFormatter.string(from: currentDate)
            if let year = components.year {
                dayLabel.text = String(format: "%ld", day)
                monthYearLabel.text = monthAbbreviation + "." + String(format: "%ld", year)
            }
        }
        if let currentData = currentData {
            enLabel.text = currentData.en
            cnLabel.text = currentData.cn
        }
    }
    
    
    @IBAction func goBack(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func didRefresh(_ sender: UIButton) {
        let bgRandomIndex = Int.random(in: 0..<resourceDatas.count)
        currentResource = resourceDatas[bgRandomIndex]
        bgView.image = UIImage(named: currentResource["bg"] as! String)
        iconView.image = UIImage(named: currentResource["icon"] as! String)
        guard !datas.isEmpty else { return }
        let iconRandomIndex = Int.random(in: 0..<datas.count)
        currentData = datas[iconRandomIndex]
        if let currentData = currentData {
            enLabel.text = currentData.en
            cnLabel.text = currentData.cn
        }
    }
    
    @IBAction func didTranslate(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        self.cnLabel.isHidden = !sender.isSelected
    }
    
    @IBAction func didShare(_ sender: UIButton) {
        if let currentData = currentData {
            if translateButton.isSelected {
                UIPasteboard.general.string = "\(currentData.en ?? "")\n\(currentData.cn ?? "")"
            }else {
                UIPasteboard.general.string = "\(currentData.en ?? "")"
            }
            self.view.makeToast("已复制，加到粘贴板", position: .center)
        }
    }
    
}
